﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;
using System.Windows.Forms;


namespace SecuritySet
{
    [RunInstaller(true)]
    public partial class SecuritySet : Installer
    {
        string keyName = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0";
        string valueName = "SharePoint";

        public SecuritySet()
        {
            InitializeComponent();
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Install(System.Collections.IDictionary stateSaver)
        {            
            // Call the base implementation.
            base.Install(stateSaver);

            string targetDir = this.Context.Parameters["targetDir"];              

            if (String.IsNullOrEmpty(targetDir))
                throw new InstallException("Cannot set the security policy. The specified target directory is not valid.");            

            try
            {
                ExecutePowerShellScript(targetDir, "sharepointfarminfotimerjobInstall");
            }
            catch (Exception ex)
            {
                throw new InstallException("Cannot set the security policy.", ex);
            }
        }       

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Rollback(System.Collections.IDictionary savedState)
        {
            // Call the base implementation.
            base.Rollback(savedState);           

            string targetDir = this.Context.Parameters["targetDir"];

            if (String.IsNullOrEmpty(targetDir))
                throw new InstallException("Cannot set the security policy. The specified target directory is not valid.");
            
            try
            {
                ExecutePowerShellScript(targetDir, "sharepointfarminfotimerjobUninstall");                
            }
            catch (Exception ex)
            {
                throw new InstallException("Cannot set the security policy.", ex);
            }

            
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            // Call the base implementation.
            base.Uninstall(savedState);

            string targetDir = this.Context.Parameters["targetDir"];

            if (String.IsNullOrEmpty(targetDir))
                throw new InstallException("Cannot set the security policy. The specified target directory is not valid.");

            try
            {
                ExecutePowerShellScript(targetDir, "sharepointfarminfotimerjobUninstall");   
            }
            catch (Exception ex)
            {
                throw new InstallException("Cannot set the security policy.", ex);
            }
        }

        /// <summary>
        /// Execute Powershell Script
        /// </summary>
        /// <param name="targetDir">Powershell script folder location</param>
        /// <param name="scriptName">PowerShell script name</param>
        private static void ExecutePowerShellScript(string targetDir, string scriptName)
        {
            Process scriptProc = new Process();
            scriptProc.StartInfo.FileName = @"PowerShell.exe";
            scriptProc.StartInfo.Arguments = String.Format(" -ExecutionPolicy Bypass -Command \"&'{0}\\{1}.ps1'\"", targetDir, scriptName);
            scriptProc.StartInfo.CreateNoWindow = true;
            scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            scriptProc.Start();
            scriptProc.WaitForExit();
            scriptProc.Close();
        }
    }
}
